{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Two-Line Element Set\n", "\n", "Examples showing how to use two-line element sets to generate a state vector and get geodetic position of satellite" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Generate State Vector" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import satkit as sk\n", "\n", "# The two-line element set\n", "# Lets pick a random StarLink satellite\n", "# The lines below were downloaded from https://www.celetrack.org\n", "tle_lines = [\n", " '0 STARLINK-30477',\n", " '1 57912U 23146X 24099.49439401 .00006757 00000+0 51475-3 0 9997',\n", " '2 57912 43.0018 157.5807 0001420 272.5369 87.5310 15.02537576 31746'\n", "]\n", "\n", "# Create a TLE object\n", "starlink30477 = sk.TLE.from_lines(tle_lines)\n", "\n", "# We want the orbital state at April 9 2024, 12:00pm UTC\n", "thetime = sk.time(2024, 4, 9, 12, 0, 0)\n", "\n", "# The state is output in the \"TEME\" frame, which is an approximate inertial\n", "# frame that does not include precession or nutation\n", "# pTEME is geocentric position in meters\n", "# vTEME is geocentric velocity in meters / second\n", "# for now we will ignore the velocity\n", "pTEME, _vTEME = sk.sgp4(starlink30477, thetime)\n", "\n", "# Suppose we want currrent latitude, longitude, and altitude of satellite:\n", "# we need to rotate into an Earth-fixed frame, the ITRF\n", "# We use a \"quaternion\" to represent the rotation. Quaternion rotations\n", "# in the satkit toolbox can be represented as multiplications of a 3-vector\n", "pITRF = sk.frametransform.qteme2itrf(thetime) * pTEME\n", "\n", "# Now lets make a \"ITRFCoord\" object to extract geodetic coordinates\n", "coord = sk.itrfcoord(pITRF)\n", "\n", "# Get the latitude, longitude, and \n", "# altitude (height above ellipsoid, or hae) of the satellite\n", "print(coord)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Plot satellite ground track" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import satkit as sk\n", "import numpy as np\n", "import plotly.graph_objects as go\n", "\n", "# The two-line element set\n", "# Same satellite as above...\n", "# The lines below were downloaded from https://www.celetrack.org\n", "tle_lines = [\n", " '0 STARLINK-30477',\n", " '1 57912U 23146X 24099.49439401 .00006757 00000+0 51475-3 0 9997',\n", " '2 57912 43.0018 157.5807 0001420 272.5369 87.5310 15.02537576 31746'\n", "]\n", "\n", "# Create a TLE object\n", "starlink30477 = sk.TLE.from_lines(tle_lines)\n", "\n", "# We want the orbital state at April 9 2024, 12:00pm UTC\n", "thetime = sk.time(2024, 4, 9, 12, 0, 0)\n", "\n", "# plot for 5 orbits. The mean motion in the TLE is number of orbits in a day\n", "timearr = np.array([thetime + sk.duration(days=x) for x in np.linspace(0, 5/starlink30477.mean_motion, 1000)])\n", "\n", "# Get position in the TEME frame\n", "pTEME, _vTEME = sk.sgp4(starlink30477, timearr)\n", "qarr = sk.frametransform.qteme2itrf(timearr)\n", "pITRF = np.array([q * p for q, p in zip(qarr, pTEME)])\n", "coord = [sk.itrfcoord(p) for p in pITRF]\n", "lat, lon, alt = zip(*[(c.latitude_deg, c.longitude_deg, c.altitude) for c in coord])\n", "\n", "fig = go.Figure()\n", "fig.add_trace(go.Scattergeo(lat=lat, lon=lon, mode='lines'))\n", "fig.update_layout(margin={\"r\":0,\"t\":40,\"l\":0,\"b\":0}, title='Ground Track', geo=dict(showland=True, showcountries=True))\n", "fig.show()\n", "fig = go.Figure()\n", "fig.add_trace(go.Scatter(x=[t.datetime() for t in timearr], y=np.array(alt)/1e3, mode='lines'))\n", "fig.update_layout(yaxis_title='Altitude (km)', xaxis_title='Time', font=dict(size=14), title='Altitude vs Time')\n", "fig.show()" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.13.0" } }, "nbformat": 4, "nbformat_minor": 2 }